package com.learn.mvc.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class Demo01GetOriginalObjHandler {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    // 获取 servletContext 对象的方法二：从IOC容器中注入
    @Autowired
    ServletContext servletContext;
    // @RequestMapping("/")
    // public String showPortal() {
    //     return "portal";
    // }

    // @RequestMapping("/index.html")
    // public String showIndex() {
    //
    //     return "portal";
    // }

    @RequestMapping("/original/api/direct")
    public String getOriginalAPIDirect(

            // 有需要使用的 Servlet API 直接在形参位置声明即可
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session

            // ServletContext 无法通过形参直接注入
            // ServletContext servletContext
    ) {

        logger.debug(request.toString());
        logger.debug(response.toString());
        logger.debug(session.toString());

        return "target";
    }

    @RequestMapping("/original/servlet/context/first/way")
    public String originalServletContextFirstWay(HttpSession session) {
        // 获取ServletContext对象的方式一：通过 HttpSession 对象获取 servletContext
        ServletContext servletContext = session.getServletContext();

        logger.debug(servletContext.toString());

        return "target";
    }

    @RequestMapping("/original/servlet/context/second/way")
    public String originalServletContextSecondWay() {

        logger.debug(this.servletContext.toString());

        return "target";
    }
}
